package com.tianqu.tms.www.rental.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.rental.actionform.RentalLocationAddInputForm;
import com.tianqu.tms.www.rental.controller.RentalLocationDetailController;
import com.tianqu.tms.www.rental.viewhelper.RentalLocationViewHelper;

/**
 * 租位修改/删除 - 信息录入
 * @author mufeiyu
 *
 */
public class RentalLocationAlterInputAction extends TmsAction {

	
	private Logger logger = Logger.getLogger(RentalLocationAlterInputAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		
		logger.debug(this.getClass().getName() + "#execute was called.");
		if(uc.getAttribute(TmsConstants.VIEWHELP)!=null){
			uc.removeAttribute(TmsConstants.VIEWHELP);
		}

		RentalLocationViewHelper rentalLocationViewHelper = new RentalLocationViewHelper();
		
		if (!StringUtil.isNullOrBlank((String)uc.getAttribute("rentalLocationAlterInputFlg"))
				&& "1".equals(uc.getAttribute("rentalLocationAlterInputFlg"))) {
			
			rentalLocationViewHelper.setId(Long.valueOf(uc.getParameter("id")));
			rentalLocationViewHelper.setLocationSerial(uc.getParameter("locationSerial"));
			rentalLocationViewHelper.setRentalType(StringUtil.getint(uc.getParameter("rentalType")));
			rentalLocationViewHelper.setAcreage(uc.getParameter("acreage"));
			rentalLocationViewHelper.setCarPort(uc.getParameter("carPort"));
			rentalLocationViewHelper.setAddress(uc.getParameter("address"));
			rentalLocationViewHelper.setRentalArea(StringUtil.getint(uc.getParameter("rentalArea")));

		} else {
			
			// 信息查看
			RentalLocationDetailController controller = new RentalLocationDetailController();
			controller.execute(uc);
			
			rentalLocationViewHelper = (RentalLocationViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);

			uc.setAttribute("rentalLocationAlterInputFlg","1");
		}
		
		//租位类型下拉列
		Codes code = Codes.getInstance();
		List codeList  = code.getCodeslistByItemId(Codes.RENTAL_TYPE);
		rentalLocationViewHelper.setRentalTypeList(codeList);
		
		List rentalAreaList  = code.getCodeslistByItemId(Codes.RENTAL_AREA);
		rentalLocationViewHelper.setRentalAreaList(rentalAreaList);
		
		uc.setAttribute(TmsConstants.VIEWHELP,rentalLocationViewHelper);
		request.setAttribute(TmsConstants.VIEWHELP,rentalLocationViewHelper);

		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
